package com.joizhang.seckill.controller;

import com.joizhang.seckill.api.service.GoodsService;
import com.joizhang.seckill.api.service.OrderService;
import com.joizhang.seckill.api.service.UserService;
import com.joizhang.seckill.api.vo.GoodsVo;
import com.joizhang.seckill.api.vo.OrderDetailVo;
import com.joizhang.seckill.api.vo.OrderInfoVo;
import com.joizhang.seckill.api.vo.UserVo;
import com.joizhang.seckill.utils.Result;
import com.joizhang.seckill.utils.WebCodeMsg;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/order")
public class OrderController {

    @DubboReference
    private UserService userService;

    @DubboReference
    private OrderService orderService;

    @DubboReference
    private GoodsService goodsService;


    @RequestMapping("/detail")
    @ResponseBody
    public Result<OrderDetailVo> getOrderDetail(UserVo userVo, @RequestParam("orderId") long orderId) {
        if (userVo == null) {
            return Result.error(WebCodeMsg.SESSION_ERROR);
        }
        OrderInfoVo order = orderService.getOrderInfoById(orderId);
        if (order == null) {
            return Result.error(WebCodeMsg.ORDER_NOT_EXIST);
        }
        long goodsId = order.getGoodsId();
        GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
        OrderDetailVo vo = new OrderDetailVo();
        vo.setOrder(order);
        vo.setGoods(goods);
        return Result.success(vo);
    }

}
